package com.controller;

import com.domain.*;
import com.service.CourseService;
import com.service.StudentService;
import com.service.TeacherService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;

    /**
     * 教师授课的课程
     * @param request
     * @return
     */
    @RequestMapping("/showCourse")
    public String showCourse(String page,HttpServletRequest request){
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        PageBean pageBean = courseService.findAndSearchCourse(currentPage,null,user.getRole(),Integer.parseInt(user.getUserName()));
        request.setAttribute("t_courses",pageBean.getList());
        request.setAttribute("pageBean",pageBean);
        return "teacher/showCourse";
    }
    /**
     * 模糊查询
     * @return
     */
    @RequestMapping("/searchCourse")
    public String searchCourse(String page,String findByName,HttpServletRequest request){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        PageBean bean = courseService.findAndSearchCourse(Integer.parseInt(page),findByName,user.getRole(),Integer.parseInt(user.getUserName()));
        request.setAttribute("tCourses",bean.getList());
        request.setAttribute("pageBean",bean);
        return "teacher/showCourse";
    }

    /**
     * 查看本门课程所有学生成绩
     * @return
     */
    @RequestMapping("/gradeCourse")
    public String gradeCourse(String page,String cid,HttpServletRequest request){
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        int id = Integer.parseInt(cid);
        PageBean bean = teacherService.findStuByCid(currentPage, Integer.parseInt(cid));
        request.getSession().setAttribute("students",bean.getList());
        request.setAttribute("cid",cid);
        request.setAttribute("pageBean",bean);
        return "teacher/showGrade";
    }

    /**
     * 给学生打分
     * @return
     */
    @RequestMapping("/mark")
    public String mark(String mark,HttpServletRequest request){
        String cid = (String) request.getSession().getAttribute("cid");
        Student student = (Student) request.getSession().getAttribute("stu");
        SelectCourse course = new SelectCourse();
        course.setCourseID(Integer.parseInt(cid));
        course.setStudentID(student.getUserID());
        course.setMark(Integer.parseInt(mark));
        teacherService.markStu(course);
        return "redirect:/teacher/gradeCourse?cid=" + cid;
    }
    @RequestMapping("/toMark")
    public String toMark(String studentId,String courseId,HttpServletRequest request){
        request.getSession().setAttribute("cid",courseId);
        Student stu = studentService.findById(studentId);
        request.getSession().setAttribute("stu",stu);
        return "teacher/mark";
    }
}
